import {request, handleFetchResultNotification, REQUEST_TYPE} from '../utils';
import {formatMessage} from './intl';
import stringify from 'Shared/utils/stringify';
import {InitData, List, Detail, Brands} from './DTO';
const baseUrl = '/afterSales/api/v1';
import {baseDataStatus} from '../Enum';
const resourceName = formatMessage({
    id: 'api.resourceName',
    defaultMessage: '索赔结算形式发票'
});

const getListDesc = formatMessage({
    id: 'api.message.getListDesc',
    defaultMessage: '查询{resourceName}'
}, {resourceName});

const abandonDesc = formatMessage({
    id: 'api.message.abandonDesc',
    defaultMessage: '作废{resourceName}'
}, {resourceName});

const confirmDesc = formatMessage({
    id: 'api.message.confirmDesc',
    defaultMessage: '开票确认'
});

const exportDesc = formatMessage({
    id: 'api.exportDesc',
    defaultMessage: '导出{resourceName}'
}, {resourceName});

const getDetailDesc = formatMessage({
    id: 'api.message.getDetailDesc',
    defaultMessage: '查询{resourceName}详情'
}, {resourceName});


const passDesc = formatMessage({
    id: 'api.message.passDesc',
    defaultMessage: '审核通过'
});

const rejectDesc = formatMessage({
    id: 'api.message.rejectDesc',
    defaultMessage: '审核不通过'
});

// 初始化查询
export const initQuery = () => request(`${baseUrl}/ui/claimProformaInvoice/init`, null, InitData)
    .then(handleFetchResultNotification(REQUEST_TYPE.query, initQuery));

export const getBrands = () => {
    const queryCondition = stringify({
        status: baseDataStatus.生效
    });
    return request(`${baseUrl}/brands?${queryCondition}`, null, Brands)
        .then(handleFetchResultNotification(REQUEST_TYPE.query, initQuery));
};
// 查询
export const getList = queryCondition => {
    const queryString = stringify(queryCondition);
    return request(`${baseUrl}/claimProformaInvoices/forCurrentUser?${queryString}`, null, List)
        .then(handleFetchResultNotification(REQUEST_TYPE.query, getListDesc));
};

// 作废
export const abandonInfo = (id, data) => request(`${baseUrl}/claimProformaInvoices/${id}/abandon`, {
    method: 'PUT',
    headers: {
        'Content-Type': 'application/json'
    },
    body: JSON.stringify(data)
}).then(handleFetchResultNotification(REQUEST_TYPE.submit, abandonDesc));

// 开票确认
export const confirmInfo = (id, data) => request(`${baseUrl}/claimProformaInvoices/${id}/confirm`, {
    method: 'PUT',
    headers: {
        'Content-Type': 'application/json'
    },
    body: JSON.stringify(data)
}).then(handleFetchResultNotification(REQUEST_TYPE.submit, confirmDesc));

// 单条导出
export const exportDetailInfo = id => request(`${baseUrl}/claimProformaInvoices/${id}/export`)
    .then(handleFetchResultNotification(REQUEST_TYPE.submit, exportDesc))
    .then(res => res.data);

// 查询详情
export const getDetail = id => request(`${baseUrl}/claimProformaInvoices/${id}`, null, Detail)
    .then(handleFetchResultNotification(REQUEST_TYPE.initQuery, getDetailDesc));


// 审核通过
export const passInfo = (id, data) => request(`${baseUrl}/claimProformaInvoices/${id}/pass`, {
    method: 'PUT',
    headers: {
        'Content-Type': 'application/json'
    },
    body: JSON.stringify(data)
}).then(handleFetchResultNotification(REQUEST_TYPE.submit, passDesc));

// 审核不通过
export const rejectInfo = (id, data) => request(`${baseUrl}/claimProformaInvoices/${id}/reject`, {
    method: 'PUT',
    headers: {
        'Content-Type': 'application/json'
    },
    body: JSON.stringify(data)
}).then(handleFetchResultNotification(REQUEST_TYPE.submit, rejectDesc));
//导出
// eslint-disable-next-line max-len
export const exportData = id => request(`${baseUrl}/claimProformaInvoices/${id}/export`, null).then(handleFetchResultNotification(REQUEST_TYPE.submit, exportDesc));
